package com.lee.dutylower.activity;


import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.lee.dutylower.BaseActivity;
import com.lee.dutylower.R;
import com.lee.dutylower.view.setting.SentrySettingFragment;
import com.lee.dutylower.view.setting.SoldierSettingFragment;
import com.lee.dutylower.view.setting.SquadSettingFragment;
import com.lee.dutylower.view.setting.TimesSettingFragment;

import java.util.LinkedList;

public class SettingActivity extends BaseActivity {
    private RadioGroup rgSetting;
    private RadioButton rbSquad;
    private RadioButton rbSentry;
    private RadioButton rbSoldier;
    private RadioButton rbTime;
    private LinkedList<RadioButton> radioButtons;
    private LinkedList<Fragment> fragments;

    private SquadSettingFragment squadSettingFragment;
    private SoldierSettingFragment soldierSettingFragment;
    private TimesSettingFragment timesSettingFragment;
    private SentrySettingFragment sentrySettingFragment;

    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settiong);
        fragmentManager = getFragmentManager();
        initView();
    }
    private void initView(){
        radioButtons = new LinkedList<>();
        fragments = new LinkedList<>();

        squadSettingFragment = new SquadSettingFragment();
        soldierSettingFragment = new SoldierSettingFragment();
        timesSettingFragment = new TimesSettingFragment();
        sentrySettingFragment = new SentrySettingFragment();

        fragments.add(squadSettingFragment);
        fragments.add(soldierSettingFragment);
        fragments.add(timesSettingFragment);
        fragments.add(sentrySettingFragment);

        rgSetting = (RadioGroup) findViewById(R.id.rg_setting);
        rbSquad = (RadioButton) findViewById(R.id.rb_setting_squad);
        rbSentry = (RadioButton) findViewById(R.id.rb_setting_sentry);
        rbSoldier = (RadioButton) findViewById(R.id.rb_setting_soldier);
        rbTime = (RadioButton) findViewById(R.id.rb_setting_time);
        radioButtons.add(rbSquad);
        radioButtons.add(rbSentry);
        radioButtons.add(rbSoldier);
        radioButtons.add(rbTime);
        rgSetting.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_setting_squad:
                        checkRadioButton(rbSquad);
                        changeFragment(squadSettingFragment,"SQUAD");
                        break;
                    case R.id.rb_setting_sentry:
                        checkRadioButton(rbSentry);
                        changeFragment(sentrySettingFragment,"SENTRY");
                        break;
                    case R.id.rb_setting_soldier:
                        checkRadioButton(rbSoldier);
                        changeFragment(soldierSettingFragment,"SOLDIER");
                        break;
                    case R.id.rb_setting_time:
                        checkRadioButton(rbTime);
                        changeFragment(timesSettingFragment,"TIMES");
                        break;
                }
            }
        });
    }

    //点击事件 切换状态
    private void checkRadioButton(RadioButton rb){
        rb.setTextColor(Color.CYAN);
        rb.setTextSize(20);
        if (radioButtons.contains(rb)){
            radioButtons.remove(rb);
        }
        for (RadioButton radioButton : radioButtons) {
            radioButton.setTextColor(Color.BLACK);
            radioButton.setTextSize(16);
        }
        radioButtons.add(rb);
    }

    //切换fragment
    private void changeFragment(Fragment selfFragment, String key){
        if(!selfFragment.isAdded()) {
            fragments.remove(selfFragment);
            for (Fragment fragment : fragments) {
                if(fragment.isAdded())
                    fragmentManager.beginTransaction().remove(fragment).commit();
            }
            fragmentManager.beginTransaction().add(R.id.ll4fragment, selfFragment, key).commit();
            fragments.add(selfFragment);
        }
    }
}
